import 'package:syria_sports/presentation/dashboard_container_screen/dashboard_container_screen.dart';
import '../../../../core/utils/validation_functions.dart';
import '../../../../data/models/auth/user.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/custom_elevated_button.dart';
import '../../../../widgets/custom_text_form_field.dart';
import '../../../../widgets/gender_selection.dart';
import 'add_subscription_requests_cubit.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';

class AddSubscriptionRequestsScreen extends StatelessWidget {
  final User? coach;
  const AddSubscriptionRequestsScreen({Key? key, this.coach}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: _buildAppBar(context),
      body: BlocProvider<AddSubscriptionRequestsCubit>(
        create: (context) => AddSubscriptionRequestsCubit()..init(coach),
        child: BlocConsumer<AddSubscriptionRequestsCubit, AddSubscriptionRequestsState>(
          listener: (context, state) {
            // TODO: implement listener
            if(state is AddAdminRegisterDone){
              Navigator.of(context, rootNavigator: true).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
                  DashboardContainerScreen.builder(context)), (Route<dynamic> route) => false);
            }
          },
          builder: (context, state) {
            ImageConstant;
            var cubit = AddSubscriptionRequestsCubit.get(context);

            return SizedBox(
              height: height,
              width: width,
              child: Column(
                children: [
                  Expanded(
                    child: Form(
                        key: cubit.formKey,
                        child: SingleChildScrollView(
                          physics: const BouncingScrollPhysics(),
                          child: Container(
                              width: double.maxFinite,
                              padding:
                              EdgeInsets.only(left: 16.h, top: 32.v, right: 16.h),
                              child: Column(
                                children: [
                                  Align(
                                    alignment: Alignment.centerRight,
                                    child: Padding(
                                      padding: const EdgeInsets.all(4.0),
                                      child: Text("هل تدير ناديًا رياضيًا وتسعى للارتقاء بخدماتك إلى مستوى أعلى؟ انضم إلى تطبيقنا المميز الذي يتيح لك إدارة ناديك بكل سهولة وفعالية. استفد من الأدوات المتقدمة التي نوفرها لتبسيط العمليات اليومية، زيادة التواصل مع الأعضاء، وتحقيق أفضل النتائج.\n انضم الآن وكن جزءًا من مجتمعنا المتنامي، حيث النجاح يبدأ بالتنظيم الذكي والإدارة المحترفة. لا تفوت فرصة الاستفادة من خدماتنا الرائدة!",style: theme.textTheme.bodySmall!.copyWith(fontSize: 16),),
                                    ),
                                  ),
                                  SizedBox(height: 10.v),
                                  // Full Name
                                  CustomTextFormField(
                                    controller: cubit.fullNameController,
                                    focusNode:cubit.fullNameFocusNode,
                                    onFieldSubmitted: (value){
                                      FocusScope.of(context).unfocus();
                                      FocusScope.of(context).requestFocus(cubit.barcodeFocusNode);
                                    },
                                    textInputAction: TextInputAction.next,
                                    hintText: "ادخل الاسم الكامل",
                                    title: 'الاسم الكامل',
                                    textInputType: TextInputType.name,
                                    prefix: Container(
                                      margin: EdgeInsets.fromLTRB(
                                        16.h, 12.v, 10.h, 12.v,
                                      ),
                                      child: CustomImageView(
                                        imagePath: ImageConstant.imgUser,
                                        height: 24.adaptSize,
                                        width: 24.adaptSize,
                                      ),
                                    ),
                                    prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return 'هذه الخانة الزامية!';
                                      }
                                      return null;
                                    },
                                    contentPadding: EdgeInsets.only(
                                      top: 15.v,
                                      right: 30.h,
                                      bottom: 15.v,
                                    ),
                                  ),
                                  SizedBox(height: 10.v),
                                  // Phone
                                  CustomTextFormField(
                                      controller: cubit.phoneController,
                                      focusNode:cubit.phoneFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        FocusScope.of(context).requestFocus(cubit.emailFocusNode);
                                      },
                                      hintText: "ادخل رقم الهاتف",
                                      title: 'رقم الجوال',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.phone,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.imgMail,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if(value == null || value.length!=10 || value.substring(0,2)!= '09') {
                                          return "err_msg_please_enter_valid_phone_number".tr;
                                        }
                                        return null;
                                      },

                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  SizedBox(height: 10.v),
                                ],
                              ),
                          ),
                        )),
                  ),
                  Row(
                    children: [
                      Expanded(
                        flex: 2,
                        child: CustomElevatedButton(
                            height: 45,
                            text:cubit.editMode ?"حفظ" : "إضافة",
                            buttonTextStyle: CustomTextStyles.titleSmallOnPrimaryContainer,
                            margin:const EdgeInsets.all(16),
                            onPressed: () {
                              FocusScope.of(context).unfocus();

                              if(cubit.formKey.currentState!.validate()){
                                if(cubit.editMode) {
                                  cubit.updateUser(context);
                                } else {
                                  cubit.addUser(context);
                                }
                              }
                              //onTapSignIn(context);
                            }),
                      ),
                      Expanded(
                        flex: 2,
                        child: CustomElevatedButton(
                            height: 45,
                            text:"رجوع",
                            buttonStyle: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all<Color>(Colors.redAccent), // Set the desired background color here
                            ),
                            margin:const EdgeInsets.all(16),
                            buttonTextStyle: CustomTextStyles.titleSmallOnPrimaryContainer,
                            onPressed: () {
                              Navigator.pop(context);
                            }),
                      ),
                    ],
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar(BuildContext context) {
    return CustomAppBar(
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "طلب اشتراك", margin: EdgeInsets.only(left: 12.h)));
  }
}
